<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="分类id" prop="categoryId">
				<!-- <el-select v-model="queryParams.categoryId" placeholder="请选择分类" clearable @change="handleQuery">
					<el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id"></el-option>
				</el-select> -->
				<el-cascader filterable :props="{label:'name',value:'id',checkStrictly: false}" clearable v-model="queryParams.categoryId" :options="typeList" @change="handleQuery"></el-cascader>

			</el-form-item>
			<el-form-item label="名称" prop="name">
				<el-input v-model="queryParams.name" placeholder="请输入名称" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="手机号码" prop="tel">
				<el-input v-model="queryParams.tel" placeholder="请输入手机号码" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="地址" prop="address">
				<el-input v-model="queryParams.address" placeholder="请输入地址" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>

			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['biz:store:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['biz:store:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['biz:store:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['biz:store:export']">导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="storeList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="ID" align="center" prop="id" />
			<el-table-column label="图片" align="center" prop="imgUrl">
				<template slot-scope="scope">
					<img :src="$store.state.app.imgUrl +scope.row.logoUrl" style="width:100px" alt="">
				</template>
			</el-table-column>
			<el-table-column label="名称" align="center" prop="name" />
			<el-table-column label="管理员" align="center" prop="userNickName" />
			<el-table-column label="手机号码" align="center" prop="tel" />
			<el-table-column label="分类" align="center" prop="categoryName" />
			<el-table-column label="地址" align="center" prop="address" />
			<!-- <el-table-column label="营业时间" align="center" prop="openTime" /> -->
			<!-- <el-table-column label="logo" align="center" prop="logoUrl" /> -->
			<el-table-column label="剩余积分" align="center" prop="remainPoints" />
			<el-table-column label="是否推荐" align="center" width="100" fixed="right">
				<template slot-scope="scope">
					<el-switch v-model="scope.row.recommend" :active-value="1" :inactive-value="0" @change="handleStatusChange2(scope.row)"></el-switch>
				</template>
			</el-table-column>
			<el-table-column label="状态" align="center" width="100" fixed="right">
				<template slot-scope="scope">
					<el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0" @change="handleStatusChange(scope.row)"></el-switch>
				</template>
			</el-table-column>
			<el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['biz:store:edit']">修改</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['biz:store:remove']">删除</el-button>
					<el-button size="mini" type="text" icon="el-icon-download" @click="qrCode(scope.row)" v-hasPermi="['biz:store:edit']">下载积分回收码</el-button>
					<el-button size="mini" type="text" icon="el-icon-download" @click="qrCode2(scope.row)" v-hasPermi="['biz:store:edit']">下载用户邀请码</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

		<!-- 添加或修改抵扣门店管理对话框 -->
		<el-dialog :title="title" :visible.sync="open" :close-on-click-modal='false' width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="auto">
				<el-form-item label="名称" prop="name">
					<el-input v-model="form.name" placeholder="请输入名称" />
				</el-form-item>
				<el-form-item label="管理员" prop="name">
					<el-select :loading="loading" :remote-method="remoteMethod" clearable="" remote filterable v-model="form.userId" placeholder="请选择">
						<el-option v-for="item in userList" :key="item.userId" :label="item.nickName+'('+item.phonenumber+')'" :value="item.userId">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="门店类型">
					<el-select v-model="form.type" placeholder="请选择">
						<el-option label="线下自营门店" :value="5"></el-option>
						<el-option label="回收门店" :value="1"></el-option>
						<el-option label="线下自营门店和回收门店" :value="6"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="提货方式">
					<el-select v-model="form.pickupMethod" placeholder="请选择">
						<el-option label="只支持自提" :value="0"></el-option>
						<el-option label="只支持快递" :value="1"></el-option>
						<el-option label="全部支持" :value="2"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="功能">
					<el-select multiple v-model="form.openedFunctions" placeholder="请选择分类">
						<el-option label="积分抵扣" value="1"></el-option>
						<el-option label="消费补贴" value="2" disabled></el-option>
						<el-option label="赠送抽奖" value="3"></el-option>
						<el-option label="赠送积分" value="4"></el-option>
						<el-option label="抵扣卡券" value="5"></el-option>
						<el-option label="赠送投资" value="6"></el-option>
					</el-select>

				</el-form-item>
				<el-form-item label="线下门店分类">
					<!-- <el-select v-model="form.categoryId" placeholder="请选择分类">
						<el-option v-for="item in typeList" :key="item.id.toString()" :label="item.name" :value="item.id.toString()"></el-option>
					</el-select> -->
					<el-cascader filterable :props="{label:'name',value:'id',checkStrictly: false}" v-model="form.categoryId" :options="typeList"></el-cascader>

				</el-form-item>
				<el-form-item label="回收门店分类">
					<el-select v-model="form.recycleCategoryId" placeholder="请选择分类">
						<el-option v-for="item in recycleTypeList" :key="item.id.toString()" :label="item.name" :value="item.id.toString()"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="描述" prop="tel">
					<el-input v-model="form.remark" placeholder="请输入描述" />
				</el-form-item>
				<el-form-item v-if="form.openedFunctions.indexOf('1') != -1" label="积分抵扣百分比(%)" prop="storeIntDeductionRate">
					<el-input v-model="form.deductionPercentage" type="number" placeholder="" />
				</el-form-item>
				<!-- <el-form-item v-if="form.openedFunctions.indexOf('2') != -1" label="消费补贴返利利率(%)" prop="storeSubsidyRebateRate">
					<el-input v-model="form.storeSubsidyRebateRate" type="number" placeholder="" />
				</el-form-item> -->
				<el-form-item v-if="form.openedFunctions.indexOf('2') != -1" label="内部会员商品额度" prop="subsidyLocalVipGoodsAmount">
					<el-input v-model="form.subsidyLocalVipGoodsAmount" type="number" placeholder="" />
				</el-form-item>
				<el-form-item v-if="form.openedFunctions.indexOf('2') != -1" label="外部会员商品额度" prop="subsidyAllVipGoodsAmount">
					<el-input v-model="form.subsidyAllVipGoodsAmount" type="number" placeholder="" />
				</el-form-item>
				<el-form-item label="手机号码" prop="tel">
					<el-input v-model="form.tel" type="number" placeholder="请输入手机号码" />
				</el-form-item>
				<!-- <el-form-item label="抵扣百分比(%)" prop="deductionPercentage" >
					<el-input v-model="form.deductionPercentage" type="number" placeholder="请输入" />
				</el-form-item> -->
				<el-form-item label="地址" :required="true">
					<el-input v-model="form.address" placeholder="请输入地址"></el-input>
					<!-- {{inputModel.addr}} -->
					<el-button size="medium" type="primary" @click="handleAddress" :loading="loading">
						选择地址
					</el-button>
				</el-form-item>
				<el-form-item label="经纬度" :required="false">
					{{form.longitude}}--{{form.latitude}}
				</el-form-item>
				<!-- <el-form-item label="营业时间" prop="openTime">
          <el-input v-model="form.openTime" placeholder="请输入营业时间" />
        </el-form-item> -->
				<el-form-item label="图片" prop="logoUrl" required>
					<ImageUpload :limit='1' ref="ImageUpload"></ImageUpload>
				</el-form-item>
				<el-form-item label="详情">
					<editor v-model="form.content" :min-height="192" />
				</el-form-item>
				<!-- <el-form-item label="剩余积分" prop="remainPoints">
					<el-input v-model="form.remainPoints" placeholder="请输入剩余积分" />
				</el-form-item> -->
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
			<addressMap ref="addressMap" @confirm='confirmAdress'></addressMap>
		</el-dialog>
	</div>
</template>

<script>
import {
	listStore,
	getStore,
	delStore,
	addStore,
	downloadQrCode,
	updateStore,
	getCategoryTree,
} from '@/api/biz/deductionStore';
import { listUser } from '@/api/system/user';
import { listRecycleCategory } from '@/api/biz/recycleCategory';
import addressMap from '@/components/addressMap';
export default {
	components: {
		addressMap,
	},
	name: 'Store',
	data() {
		return {
			// 分类列表
			typeList: [],
			recycleTypeList: [],
			userList: [],
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 抵扣门店管理表格数据
			storeList: [],
			// 弹出层标题
			title: '',
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				type: null,
				categoryId: [],
				name: null,
				tel: null,
				address: null,
				latitude: null,
				longitude: null,
				openTime: null,
				imgUrl: null,
				logoUrl: null,
				content: null,
				status: null,
				readCount: null,
				visitCount: null,
				remainPoints: null,
			},
			// 表单参数
			form: {
				openedFunctions: '',
			},
			// 表单校验
			rules: {},
		};
	},
	created() {
		this.listUser();
		this.getList();
		this.getType();
		this.listRecycleCategory();
	},
	activated() {
		this.getType();
	},
	methods: {
		// 门店推荐
		handleStatusChange2(row) {
			var params = {
				recommend: row.recommend,
				id: row.id,
			};
			updateStore(params).then((response) => {
				this.$modal.msgSuccess('修改成功');
				this.open = false;
				this.getList();
			});
		},
		// 门店上下架
		handleStatusChange(row) {
			var params = {
				status: row.status,
				id: row.id,
			};
			updateStore(params).then((response) => {
				this.$modal.msgSuccess('修改成功');
				this.open = false;
				this.getList();
			});
		},
		remoteMethod(query) {
			if (query !== '') {
				this.loading = true;
				const params = {
					phonenumber: query,
					pageSize: 10,
				};
				listUser(params).then((response) => {
					this.userList = response.rows;
					this.loading = false;
				});
			} else {
				this.userList = [];
			}
		},
		listUser(params) {
			listUser({
				pageSize: 10,
			}).then((response) => {
				this.userList = response.rows;
			});
		},
		getType() {
			getCategoryTree({}).then((response) => {
				this.typeList = response.data;
			});
		},
		listRecycleCategory() {
			listRecycleCategory({ pageSize: 999 }).then((response) => {
				this.recycleTypeList = response.rows;
			});
		},
		confirmAdress(res) {
			console.log(res);
			this.$set(this.form, 'address', res.inputModel.poiaddress);

			this.form.latitude = String(res.inputModel.latlng.lat);
			this.form.longitude = String(res.inputModel.latlng.lng);
		},
		handleAddress() {
			this.$refs.addressMap.show();
		},
		/** 查询抵扣门店管理列表 */
		getList() {
			var queryParams = JSON.parse(JSON.stringify(this.queryParams));
			queryParams.categoryId = queryParams.categoryId.toString();
			this.loading = true;
			listStore(queryParams).then((response) => {
				this.storeList = response.rows;
				this.total = response.total;
				this.loading = false;
			});
		},
		// 取消按钮
		cancel() {
			this.open = false;
			this.reset();
		},
		// 表单重置
		reset() {
			this.form = {
				openedFunctions: '',
			};
			this.resetForm('form');
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1;
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm('queryForm');
			this.handleQuery();
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map((item) => item.id);
			this.single = selection.length !== 1;
			this.multiple = !selection.length;
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset();
			this.open = true;
			this.title = '添加抵扣门店管理';
		},
		qrCode(row) {
			var url = `${this.$store.state.app.url2}/biz/store/downloadQrCode?fileName=积分收款码(${row.name}).
			jpg&pages=pages/users/payForShopPoint&parameter=${row.id}&qrDesc1=积分收款码,${row.name}`;
			window.open(url);
		},
		qrCode2(row) {
			var url = `${this.$store.state.app.url2}/biz/store/downloadQrCode?fileName=用户邀请码(${row.name}).
			jpg&pages=pages/users/login&parameter=${row.inviteCode}&qrDesc1=用户邀请码,${row.name}`;
			window.open(url);
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset();
			const id = row.id || this.ids;
			this.open = true;
			getStore(id).then((response) => {
				this.$refs.ImageUpload.fileList = [
					{
						url:
							this.$store.state.app.imgUrl +
							response.data.logoUrl,
						uploadUrl: response.data.logoUrl,
					},
				];
				response.data.categoryId = response.data.categoryId
					? response.data.categoryId.split(',')
					: [];
				response.data.categoryId.forEach((element, index) => {
					response.data.categoryId[index] = Number(element);
				});

				response.data.openedFunctions = response.data.openedFunctions
					? response.data.openedFunctions.split(',')
					: [];
				response.data.storeIntDeductionRate =
					response.data.deductionPercentage;
				this.form = response.data;

				this.title = '修改抵扣门店管理';
			});
		},
		/** 提交按钮 */
		submitForm() {
			if (this.$refs.ImageUpload.fileList.length) {
				var fileList = this.$refs.ImageUpload.fileList[0];
				this.form.logoUrl = fileList.uploadUrl;
			}
			var form = JSON.parse(JSON.stringify(this.form));
			form.categoryId = form.categoryId.toString();
			form.openedFunctions = form.openedFunctions.toString();
			delete form.categoryIdList;
			this.$refs['form'].validate((valid) => {
				if (valid) {
					if (this.form.id != null) {
						updateStore(form).then((response) => {
							this.$modal.msgSuccess('修改成功');
							this.open = false;
							this.getList();
						});
					} else {
						addStore(form).then((response) => {
							this.$modal.msgSuccess('新增成功');
							this.open = false;
							this.getList();
						});
					}
				}
			});
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const ids = row.id || this.ids;
			this.$modal
				.confirm(
					'是否确认删除抵扣门店管理编号为"' + ids + '"的数据项？'
				)
				.then(function () {
					return delStore(ids);
				})
				.then(() => {
					this.getList();
					this.$modal.msgSuccess('删除成功');
				})
				.catch(() => {});
		},
		/** 导出按钮操作 */
		handleExport() {
			this.download(
				'biz/store/export',
				{
					...this.queryParams,
				},
				`store_${new Date().getTime()}.xlsx`
			);
		},
	},
};
</script>
